#!/bin/bash

# PromptMinder API 接口测试脚本 (curl版本)
# 使用方法: ./test_api_curl.sh

BASE_URL="http://localhost:8000"
TOKEN=""

echo "🚀 PromptMinder API 测试工具 (curl版本)"
echo "请确保后端服务已启动在 $BASE_URL"
echo "=========================================="

# 1. 登录获取Token
echo "📝 步骤1: 登录获取Token"
echo "请输入邮箱:"
read EMAIL
echo "请输入密码:"
read -s PASSWORD

LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/auth/token" \
  -H "Content-Type: application/json" \
  -d "{
    \"username\": \"$EMAIL\",
    \"password\": \"$PASSWORD\"
  }")

TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"access_token":"[^"]*"' | cut -d'"' -f4)

if [ -z "$TOKEN" ]; then
    echo "❌ 登录失败，无法获取Token"
    echo "响应: $LOGIN_RESPONSE"
    exit 1
fi

echo "✅ 登录成功，已获取Token"
echo "=========================================="

# 2. 测试创建提示词
echo "🧪 步骤2: 测试创建提示词接口"
CREATE_RESPONSE=$(curl -s -X POST "$BASE_URL/prompts/createPrompts" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
          "prompt": {
        "title": "API测试提示词",
        "content": "你是一个智能助手，请帮助用户解决问题。请用友好、专业的语气回答用户的问题。",
        "description": "用于API测试的智能助手提示词",
        "version": "2.0.0",
        "tags": "API,测试,助手",
        "cover_img": null
      },
    "model": {
      "channel": "OpenAI",
      "channel_id": 1,
      "model": "gpt-3.5-turbo",
      "temperature": 0.7,
      "max_tokens": 2048,
      "top_p": 1.0,
      "presence_penalty": 0.0,
      "frequency_penalty": 0.0,
      "n": 1,
      "stop": ["\n", "。"],
      "logit_bias": null,
      "stream": true,
      "api_key": "your-api-key-here"
    }
  }')

echo "创建提示词响应:"
echo "$CREATE_RESPONSE" | jq '.'

# 提取创建的提示词ID
PROMPT_ID=$(echo $CREATE_RESPONSE | jq -r '.id // empty')
if [ -z "$PROMPT_ID" ] || [ "$PROMPT_ID" = "null" ]; then
    echo "❌ 创建提示词失败，无法获取ID"
    exit 1
fi

echo "✅ 创建提示词成功，ID: $PROMPT_ID"
echo "=========================================="

# 3. 测试分页查询
echo "🧪 步骤3: 测试分页查询接口"

# 查询所有提示词
echo "📋 查询所有提示词:"
QUERY_RESPONSE=$(curl -s -X GET "$BASE_URL/prompts/queryPrompts?page=1&page_size=10" \
  -H "Authorization: Bearer $TOKEN")
echo "$QUERY_RESPONSE" | jq '.'

# 按标签查询
echo "📋 按标签查询 (API):"
QUERY_TAG_RESPONSE=$(curl -s -X GET "$BASE_URL/prompts/queryPrompts?tag=API&page=1&page_size=10" \
  -H "Authorization: Bearer $TOKEN")
echo "$QUERY_TAG_RESPONSE" | jq '.'

# 按标题查询
echo "📋 按标题查询 (API测试):"
QUERY_TITLE_RESPONSE=$(curl -s -X GET "$BASE_URL/prompts/queryPrompts?title=API测试&page=1&page_size=10" \
  -H "Authorization: Bearer $TOKEN")
echo "$QUERY_TITLE_RESPONSE" | jq '.'

echo "✅ 分页查询测试完成"
echo "=========================================="

# 4. 测试修改提示词
echo "🧪 步骤4: 测试修改提示词接口"
UPDATE_RESPONSE=$(curl -s -X POST "$BASE_URL/prompts/updatePromptsById" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": {
      "id": "'$PROMPT_ID'",
      "title": "API测试提示词-已更新",
      "content": "你是一个智能助手，请帮助用户解决问题。请用友好、专业的语气回答用户的问题。这是更新后的内容。",
      "description": "用于API测试的智能助手提示词-已更新",
      "version": "2.1.0",
      "tags": "API,测试,助手,更新",
      "cover_img": null
    },
    "model": {
      "channel": "OpenAI",
      "model": "gpt-3.5-turbo",
      "temperature": 0.8,
      "max_tokens": 4096,
      "top_p": 0.9,
      "presence_penalty": 0.0,
      "frequency_penalty": 0.0,
      "n": 1,
      "stream": true
    }
  }')

echo "修改提示词响应:"
echo "$UPDATE_RESPONSE" | jq '.'

echo "✅ 修改提示词测试完成"
echo "=========================================="

# 5. 测试数据校验错误
echo "🧪 步骤5: 测试数据校验错误"
VALIDATION_RESPONSE=$(curl -s -X POST "$BASE_URL/prompts/createPrompts" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": {
      "title": "",
      "content": "内容",
      "version": "1.0.0"
    },
    "model": {
      "channel": "",
      "model": "gpt-3.5-turbo",
      "temperature": 3.0,
      "max_tokens": 5
    }
  }')

echo "数据校验错误响应:"
echo "$VALIDATION_RESPONSE" | jq '.'

echo "✅ 数据校验错误测试完成"
echo "=========================================="

# 6. 测试删除提示词
echo "🧪 步骤6: 测试删除提示词接口"
DELETE_RESPONSE=$(curl -s -X DELETE "$BASE_URL/prompts/deletePrompts?title=API测试提示词-已更新&version=2.1.0" \
  -H "Authorization: Bearer $TOKEN")

echo "删除提示词响应:"
echo "$DELETE_RESPONSE" | jq '.'

echo "✅ 删除提示词测试完成"
echo "=========================================="

echo "🎉 所有测试完成！"
echo ""
echo "📊 测试总结:"
echo "- ✅ 登录认证"
echo "- ✅ 创建提示词"
echo "- ✅ 分页查询"
echo "- ✅ 修改提示词"
echo "- ✅ 数据校验"
echo "- ✅ 删除提示词"
